package com.tbabs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tbabs.common.CustomException;
import com.tbabs.dto.SetmealDto;
import com.tbabs.entity.Setmeal;
import com.tbabs.entity.SetmealDish;
import com.tbabs.mapper.SetmealMapper;
import com.tbabs.service.SetmealDishService;
import com.tbabs.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    // 保存套餐和套餐菜品关系(开启事务)
    @Transactional
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        // 保存套餐基本信息
        this.save(setmealDto);

        // 获取套餐菜品关系列表
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        // 将列表中元素的setmealId设置
        setmealDishes = setmealDishes.stream().peek((item) -> item.setSetmealId(setmealDto.getId())).collect(Collectors.toList());

        // 保存套餐和菜品的关联信息
        setmealDishService.saveBatch(setmealDishes);
    }

    // 删除套餐和套餐菜品关联信息
    @Transactional
    @Override
    public void deleteWithDish(List<Long> ids) {
        // 查询套餐状态
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.in(Setmeal::getId, ids)
                .eq(Setmeal::getStatus, 1);
        long count = this.count(setmealLambdaQueryWrapper);
        if (count > 0) {
            throw new CustomException("套餐正在销售中，无法删除！");
        }
        // 批量删除套餐基本信息
        this.removeByIds(ids);
        // 批量删除套餐菜品关联信息
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SetmealDish::getSetmealId, ids);

        setmealDishService.remove(queryWrapper);
    }

    // 根据id查询套餐及菜品的相关信息
    @Override
    public SetmealDto getInfoByIDWithDish(Long id) {
        // 获取套餐基本信息
        Setmeal setmeal = this.getById(id);

        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);

        // 查询菜品信息
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        setmealDto.setSetmealDishes(list);

        return setmealDto;
    }

    // 修改套餐及套餐菜品信息
    @Transactional
    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        // 更新套餐基本信息
        this.updateById(setmealDto);
        // 清除原先的套餐菜品信息
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(queryWrapper);
        // 添加新的更新菜品信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().peek((item) -> item.setSetmealId(setmealDto.getId())).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);
    }
}
